//
//  QuWeiXunLianXuanXiangViewController.swift
//  CKC
//
//  Created by 张振东 on 16/10/5.
//  Copyright © 2016年 ZZD. All rights reserved.
//

import UIKit

class QuWeiXunLianXuanXiangViewController: UIViewController {

    //获取模拟器的宽度和高度
    let screenWidth = UIScreen.main.bounds.width
    let screenHeight = UIScreen.main.bounds.height
    var navigationHeight:CGFloat?
    
    var xuanZeXianShiMoShiFenZhongZhi:UILabel?
    
    var segmentArray:[String] = ["2","4","6"]
    
    //定义一个训练时长segment对象
    var segmentController:UISegmentedControl?

    //定义一个变量,用于保存segment选中的值
    var selectedSegmentValue:Int = 2
    
    //定义一个完成按钮
    var wanChengBtn:UIButton?

    override func viewDidLoad() {
        self.title = "限时模式"
       
        self.view.backgroundColor = UIColor(red: 246,green: 246,blue: 246,alpha: 1)
        let leftBarButton = UIButton(frame:CGRect(x: 0,y: 0,width: 20,height: 20))
        leftBarButton.setBackgroundImage(UIImage(named: "fanhui"), for: UIControlState())
        leftBarButton.addTarget(self, action: #selector(backToPrevious), for: .touchUpInside)
        let leftBarButtonItem = UIBarButtonItem(customView: leftBarButton)
        self.navigationItem.leftBarButtonItems = [leftBarButtonItem]

        //获取导航栏的高度
        navigationHeight = self.navigationController?.navigationBar.bounds.size.height
        
        //初始化小键盘训练说明
        xuanZeXianShiMoShiFenZhongZhi = UILabel(frame:CGRect(x: 10,y: navigationHeight!+50,width: 80,height: 30))
        xuanZeXianShiMoShiFenZhongZhi?.font = UIFont.systemFont(ofSize: 17)
        xuanZeXianShiMoShiFenZhongZhi?.text = "限时时长:"
        
        
        //初始化segmentController对象
        segmentController = UISegmentedControl(items:segmentArray)
        segmentController?.frame = CGRect(x: 15+80, y: navigationHeight!+50, width: screenWidth-110, height: 30)
        segmentController?.selectedSegmentIndex = 0
        segmentController?.addTarget(self, action: #selector(segmentChange), for: .valueChanged)
        
        //初始化wanChengBtn按钮
        wanChengBtn = UIButton(frame: CGRect(x: 10, y: screenHeight-120, width: screenWidth-20 , height: 40))
        wanChengBtn?.backgroundColor = UIColor(red: 231/255,green: 76/255,blue: 60/255,alpha: 1)
        wanChengBtn?.layer.cornerRadius = 10
        wanChengBtn?.setTitle("完成", for: UIControlState())
        wanChengBtn?.addTarget(self, action: #selector(wanChengSheZhi), for: .touchUpInside)
        
        self.view.addSubview(xuanZeXianShiMoShiFenZhongZhi!)
        self.view.addSubview(segmentController!)
        self.view.addSubview(wanChengBtn!)
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        //隐藏导航栏
        self.navigationController?.navigationBar.isHidden = false
    }
    
    /*
     作者:张振东
     时间:2016-10-05
     功能:当segment发生改变时,记录选中的值
     */
    func segmentChange(_ sender:Any){
        let segment = sender as! UISegmentedControl
        let selectedSegmentIndex = segment.selectedSegmentIndex
        selectedSegmentValue = Int(segmentArray[selectedSegmentIndex])!
    }

    
    /*
     作者:张振东
     时间:2016-9-28
     功能:将选中的模式值和训练时长传递给上一个界面
     */
    func wanChengSheZhi(){
        
        print(selectedSegmentValue)//成功筛选
        let quWeiXunLianMainViewController = QuWeiXunLianMainViewController()
        quWeiXunLianMainViewController.xuanZhongFenZhongZhi = selectedSegmentValue
        self.navigationController?.pushViewController(quWeiXunLianMainViewController, animated: true)
        
        
        
    }

    
    //返回到上一个页面
    func backToPrevious(){
        self.navigationController?.popViewController(animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
